using System;
using System.IO;
using System.Threading;
using CS2.Core.Parsing;
using Directory=Lucene.Net.Store.Directory;

namespace CS2.Core.Indexing
{
    public class TimedIndexingService : IIndexingService, IDisposable
    {
        inner : IIndexingService;
        timer : Timer;

        public this(inner : IIndexingService, updateInterval : TimeSpan)
        {
            this.inner = inner;
            timer = Timer(_ => UpdateIndex(), null, updateInterval, updateInterval);
        }

        public DocumentCount : int
        {
            get { inner.DocumentCount }
        }

        public Exclusions : array[string]
        {
            set { inner.Exclusions = value }
        }

        public RequestIndexing(file : FileInfo) : void
        {
            inner.RequestIndexing(file)
        }

        public RequestIndexing(directory : DirectoryInfo) : void
        {
            inner.RequestIndexing(directory)
        }

        public UpdateIndex() : void
        {
            inner.UpdateIndex()
        }

        public IndexDirectory : Directory
        {
            get { inner.IndexDirectory }
        }

        public IsWaitingForFilesToBeIndexed : bool
        {
            get { inner.IsWaitingForFilesToBeIndexed }
        }

        public DeletedFilesSinceLastUpdate : int
        {
            get { inner.DeletedFilesSinceLastUpdate }
        }

        public AddedFilesSinceLastUpdate : int
        {
            get { inner.AddedFilesSinceLastUpdate }
        }

        public Parsers : array[IParsingService]
        {
            get { inner.Parsers }
        }

        public event IndexingCompleted : Action[IndexingCompletedEventArgs]
        {
            add { inner.IndexingCompleted += value }
            remove { inner.IndexingCompleted -= value }
        }

        public Dispose() : void
        {
            Dispose(true)
        }

        protected virtual Dispose(_ : bool) : void
        {
            when(timer != null)
                timer.Dispose()
        }
    }
}